﻿Imports System.ServiceProcess
Imports System.Management


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = ""

        Label1.Text = My.Settings.servizio



        PictureBox2.Image = My.Resources.Resource1.pending.ToBitmap

        PictureBox1.Image = My.Resources.Resource1.pending.ToBitmap

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = My.Resources.Resource1.pending.ToBitmap
        'Dim imp As New RunAs_Impersonator
        'imp.ImpersonateStart(TextBox5.Text, TextBox3.Text, TextBox4.Text)
        'Dim sqlSvc As ServiceController
        'sqlSvc = New ServiceController(TextBox1.Text, TextBox2.Text)
        'If sqlSvc.Status.ToString.Equals("Running") Then
        '    RadioButton1.Checked = True

        'Else
        '    RadioButton1.Checked = False
        'End If

        'imp.ImpersonateStop()

        If (My.Settings.servizio <> "") And (My.Settings.pc <> "") Then


            Dim myConnectionOptions As New System.Management.ConnectionOptions
            With myConnectionOptions
                .Impersonation = System.Management.ImpersonationLevel.Impersonate
                '    '* Use next line for XP
                .Authentication = System.Management.AuthenticationLevel.Packet
                '    '* Use next line for Win prior XP
                '    '*.Authentication = System.Management.AuthenticationLevel.Connect
                .Username = My.Settings.user
                .Password = My.Settings.pass
            End With
            Dim path As ManagementPath = Nothing
            Dim options As ConnectionOptions = Nothing
            Dim scope As ManagementScope = Nothing
            Dim oq As System.Management.ObjectQuery
            Dim query As ManagementObjectSearcher
            Dim ms As System.Management.ManagementScope
            Dim mo As ManagementObject
            Dim queryCollection As ManagementObjectCollection
            path = New ManagementPath("\\" & My.Settings.pc & "\root\CIMV2")



            scope = New ManagementScope(path, myConnectionOptions)

            scope.Connect()
            If scope.IsConnected = False Then
                PictureBox2.Image = My.Resources.Resource1.cross.ToBitmap

            Else
                PictureBox2.Image = My.Resources.Resource1.tick.ToBitmap


                Dim strquery As String

                strquery = "SELECT * FROM Win32_Service WHERE name='" & My.Settings.servizio & "'"



                oq = New System.Management.ObjectQuery(strquery)


                query = New ManagementObjectSearcher(scope, oq)
                Try
                    queryCollection = query.Get()
                Catch e1 As Exception
                    Console.WriteLine("Error: " + e1.ToString())
                End Try
                For Each mo In queryCollection
                    ' create child node for operating system
                    If mo("state").ToString = "Running" Then
                        PictureBox1.Image = My.Resources.Resource1.running.ToBitmap
                    Else
                        PictureBox1.Image = My.Resources.Resource1.stopped.ToBitmap
                    End If
                Next

            

            End If

        Else

            MsgBox("Impostare i dati nelle opzioni")

        End If




    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        AboutBox1.Show()

    End Sub

   

   

    Private Sub ImpostazioniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpostazioniToolStripMenuItem.Click
        Form2.Show()

    End Sub

    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click

    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If (My.Settings.servizio <> "") And (My.Settings.pc <> "") Then


            Dim myConnectionOptions As New System.Management.ConnectionOptions
            With myConnectionOptions
                .Impersonation = System.Management.ImpersonationLevel.Impersonate
                '    '* Use next line for XP
                .Authentication = System.Management.AuthenticationLevel.Packet
                '    '* Use next line for Win prior XP
                '    '*.Authentication = System.Management.AuthenticationLevel.Connect
                .Username = My.Settings.user
                .Password = My.Settings.pass
            End With
            Dim path As ManagementPath = Nothing
            Dim options As ConnectionOptions = Nothing
            Dim scope As ManagementScope = Nothing
            Dim oq As System.Management.ObjectQuery
            Dim query As ManagementObjectSearcher
            Dim ms As System.Management.ManagementScope
            Dim mo As ManagementObject
            Dim queryCollection As ManagementObjectCollection
            path = New ManagementPath("\\" & My.Settings.pc & "\root\CIMV2")



            scope = New ManagementScope(path, myConnectionOptions)

            scope.Connect()
            If scope.IsConnected = False Then
                PictureBox2.Image = My.Resources.Resource1.cross.ToBitmap

            Else
                PictureBox2.Image = My.Resources.Resource1.tick.ToBitmap


                Dim strquery As String

                strquery = "SELECT * FROM Win32_Service WHERE name='" & My.Settings.servizio & "'"



                oq = New System.Management.ObjectQuery(strquery)


                query = New ManagementObjectSearcher(scope, oq)
                Try
                    queryCollection = query.Get()
                Catch e1 As Exception
                    Console.WriteLine("Error: " + e1.ToString())
                End Try
                For Each mo In queryCollection
                    ' create child node for operating system

                    Dim arg As Object

                    mo.InvokeMethod("StartService", arg)

                Next



                For Each mo In queryCollection

                    If mo("state").ToString = "Running" Then
                        PictureBox1.Image = My.Resources.Resource1.running.ToBitmap
                    Else
                        PictureBox1.Image = My.Resources.Resource1.stopped.ToBitmap
                    End If
                Next
            End If

        Else

            MsgBox("Impostare i dati nelle opzioni")

        End If


        Button1_Click(sender, New System.EventArgs())


    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim reply As DialogResult = MessageBox.Show("Vuoi davvero stoppare il servizio " & My.Settings.servizio.ToString, "Conferma arresto servizio", _
                      MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)

        If reply = DialogResult.Yes Then
            'Process Yes



            If (My.Settings.servizio <> "") And (My.Settings.pc <> "") Then


                Dim myConnectionOptions As New System.Management.ConnectionOptions
                With myConnectionOptions
                    .Impersonation = System.Management.ImpersonationLevel.Impersonate
                    '    '* Use next line for XP
                    .Authentication = System.Management.AuthenticationLevel.Packet
                    '    '* Use next line for Win prior XP
                    '    '*.Authentication = System.Management.AuthenticationLevel.Connect
                    .Username = My.Settings.user
                    .Password = My.Settings.pass
                End With
                Dim path As ManagementPath = Nothing
                Dim options As ConnectionOptions = Nothing
                Dim scope As ManagementScope = Nothing
                Dim oq As System.Management.ObjectQuery
                Dim query As ManagementObjectSearcher
                Dim ms As System.Management.ManagementScope
                Dim mo As ManagementObject
                Dim queryCollection As ManagementObjectCollection
                path = New ManagementPath("\\" & My.Settings.pc & "\root\CIMV2")



                scope = New ManagementScope(path, myConnectionOptions)

                scope.Connect()
                If scope.IsConnected = False Then
                    PictureBox2.Image = My.Resources.Resource1.cross.ToBitmap

                Else
                    PictureBox2.Image = My.Resources.Resource1.tick.ToBitmap


                    Dim strquery As String

                    strquery = "SELECT * FROM Win32_Service WHERE name='" & My.Settings.servizio & "'"



                    oq = New System.Management.ObjectQuery(strquery)


                    query = New ManagementObjectSearcher(scope, oq)
                    Try
                        queryCollection = query.Get()
                    Catch e1 As Exception
                        Console.WriteLine("Error: " + e1.ToString())
                    End Try
                    For Each mo In queryCollection
                        ' create child node for operating system

                        Dim arg As Object

                        mo.InvokeMethod("StopService", arg)






                    Next

                    For Each mo In queryCollection

                        If mo("state").ToString = "Running" Then
                            PictureBox1.Image = My.Resources.Resource1.running.ToBitmap
                        Else
                            PictureBox1.Image = My.Resources.Resource1.stopped.ToBitmap
                        End If


                    Next
                End If

            Else

                MsgBox("Impostare i dati nelle opzioni")

            End If
            Button1_Click(sender, New System.EventArgs())

        Else

        End If


    End Sub
End Class
